package com.grasscove.namethat;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

import com.grasscove.namethat.adapters.WordsDbAdapter;

public class Play extends Activity
{
	private static final int DIALOG_TIME_UP = 0;
	private static final int DIALOG_WINNER = 1;
	private static final int DIALOG_WORD_LIMIT = 2;

	private final SimpleDateFormat mFormat = new SimpleDateFormat("mm:ss");

	private WordsDbAdapter mDbAdapter;
	private Intent mIntent;
	private Cursor mCursor;
	private SharedPreferences mSettings;
	private MediaPlayer mMp;
	private CountDownTimer mTimer;

	private TextView mCategoryWord;
	private TextView mTimerView;
	private TextView mNextWord;
	private TextView mTeam1ScoreView;
	private TextView mTeam2ScoreView;
	private TextView mTeam1TextView;
	private TextView mTeam2TextView;
	private TextView mCategory;

	private int mCategoryId;
	private int mTeam1Score = 0;
	private int mTeam2Score = 0;
	private int mActiveTeam = 1;
	private int mMinutes = 0;

	private ArrayList<Integer> mUsedWords = new ArrayList<Integer>();
	private Integer wordId = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mDbAdapter = new WordsDbAdapter(this);
		mDbAdapter.open();

		setContentView(R.layout.play);

		mIntent = getIntent();
		mCategory = (TextView)findViewById(R.id.play_cat);
		mCategoryId = mIntent.getIntExtra("category", 0);
		mCategory.setText(CategoriesList.CATEGORIES[mCategoryId].toString());

		mMp = MediaPlayer.create(this, R.raw.tick);
		mMp.setLooping(true);
		mSettings = getSharedPreferences("settings", MODE_PRIVATE);

		mTeam1TextView = (TextView)findViewById(R.id.team1_text);
		mTeam2TextView = (TextView)findViewById(R.id.team2_text);
		mTeam1ScoreView = (TextView)findViewById(R.id.team1_score);
		mTeam2ScoreView = (TextView)findViewById(R.id.team2_score);
		mTimerView = (TextView)findViewById(R.id.play_timer);
		mCategoryWord = (TextView)findViewById(R.id.play_word_bubble);

		mNextWord = (TextView)findViewById(R.id.play_next_word);
		mNextWord.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int action = event.getAction();
				if(action == MotionEvent.ACTION_DOWN) {
					v.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_lg));
				} else if(action == MotionEvent.ACTION_UP) {
					v.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_lg_select));
				}
				return false;
			}
        });

		if(mIntent.hasExtra("team1Score")) {
			mTeam1ScoreView.setText(mIntent.getStringExtra("team1Score"));
		}
		if(mIntent.hasExtra("team2Score")) {
			mTeam2ScoreView.setText(mIntent.getStringExtra("team2Score"));
		}

		mTeam1ScoreView.setTextColor(Color.WHITE);
	}

	@Override
	protected void onResume() {
		super.onResume();

		mCursor = mDbAdapter.getRandomFromCategory(mCategoryId);
        startManagingCursor(mCursor);

        mCursor.moveToFirst();
        mCategoryWord.setText(mCursor.getString(mCursor.getColumnIndex(WordsDbAdapter.KEY_WORD)));

        mMinutes = mSettings.getInt("countdown_time", 0) + 1;
        mTimerView.setText(mMinutes +":00");

        if(mActiveTeam == 1) {
        	mActiveTeam = 2;
        	mTeam2ScoreView.setTextColor(Color.GREEN);
        	mTeam1ScoreView.setTextColor(Color.WHITE);

        	mTeam2TextView.setTextColor(Color.GREEN);
        	mTeam1TextView.setTextColor(Color.WHITE);
        } else {
        	mActiveTeam = 1;
        	mTeam1ScoreView.setTextColor(Color.GREEN);
        	mTeam2ScoreView.setTextColor(Color.WHITE);

        	mTeam1TextView.setTextColor(Color.GREEN);
        	mTeam2TextView.setTextColor(Color.WHITE);
        }

        if(mTimer == null) {
        	mTimer = new CountDownTimer(mMinutes * 60000, 1000) {
    	    	public void onTick(long millisUntilFinished) {
    	   	    	 if(millisUntilFinished <= 20000) {
    	   	    		 mMp.start();
    	   	    	 }
    	   	    	 mTimerView.setText(mFormat.format(millisUntilFinished));
    	   	     }

    	   	     public void onFinish() {
    	   	    	 mMp.stop();
    	   	    	 mTimerView.setText("done!");
    	   	    	 if(mActiveTeam == 2) {
    	   	        	 mTeam1ScoreView.setText(Integer.toString(++mTeam1Score));
    	   	        	 if(mTeam1Score == 7) {
    	   	        		 mTimer.cancel();
    	   	        		 showDialog(DIALOG_WINNER);
    	   	        	 } else {
    	   	        		showDialog(DIALOG_TIME_UP);
    	   	        	 }
    	   	         } else {
    	   	        	 mTeam2ScoreView.setText(Integer.toString(++mTeam2Score));
    	   	        	 if(mTeam2Score == 7) {
    	   	        		mTimer.cancel();
    	   	        		showDialog(DIALOG_WINNER);
	   	   	        	 } else {
	   	   	        		 showDialog(DIALOG_TIME_UP);
	   	   	        	 }
    	   	         }
    	   	     }
            };
    	   	mTimer.start();
        }
	}

	/**
	 * TODO: PAUSE TIMER
	 */
	@Override
	protected void onPause() {
		super.onPause();

		mMp.pause();
	}

	@Override
	protected void onStop() {
		super.onStop();

		//mMp.setVolume(0, 0);
		mMp.stop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		mTimer.cancel();
	}

	public void onClickListener(View target) {
    	switch(target.getId()) {
    		case R.id.play_next_word:
    			mCursor = mDbAdapter.getRandomFromCategory(mCategoryId);
    	        startManagingCursor(mCursor);
    	        mCursor.moveToFirst();

    	        /**
    	         * Test if the word has been used before during this round.
    	         */
    	        wordId = mCursor.getInt(mCursor.getColumnIndex(WordsDbAdapter.KEY_ID));
    	        while(mUsedWords.contains(wordId)) {
    	        	if(mUsedWords.size() > 100) {
    	        		showDialog(DIALOG_WORD_LIMIT);
    	        		return;
    	        	}
    	        	mCursor.close();
    	        	mCursor = mDbAdapter.getRandomFromCategory(mCategoryId);
    	        	mCursor.moveToFirst();
    	        	wordId = mCursor.getInt(mCursor.getColumnIndex(WordsDbAdapter.KEY_ID));
    	        }

    	        startManagingCursor(mCursor);

    	        mUsedWords.add(wordId);

    	        mCategoryWord.setText(mCursor.getString(mCursor.getColumnIndex(WordsDbAdapter.KEY_WORD)));
    	        if(mActiveTeam == 1) {
    	        	mActiveTeam = 2;
    	        	mTeam2ScoreView.setTextColor(Color.GREEN);
    	        	mTeam1ScoreView.setTextColor(Color.WHITE);

    	        	mTeam2TextView.setTextColor(Color.GREEN);
    	        	mTeam1TextView.setTextColor(Color.WHITE);
    	        } else {
    	        	mActiveTeam = 1;
    	        	mTeam1ScoreView.setTextColor(Color.GREEN);
    	        	mTeam2ScoreView.setTextColor(Color.WHITE);

    	        	mTeam1TextView.setTextColor(Color.GREEN);
    	        	mTeam2TextView.setTextColor(Color.WHITE);
    	        }
				break;
    		default:
    			break;
    	}
    }

	@Override
	public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
        	
        }
		return true;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		String team = (mActiveTeam == 1) ? "Two" : "One";

    	switch(id) {
    		case DIALOG_TIME_UP:
    			team = (mActiveTeam == 1) ? "Two" : "One";
    			builder
    				.setCancelable(false)
					.setTitle("Times up!")
					.setMessage("Team "+ team +" gained a point!")
					.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							mTimerView.setText(mMinutes +":00");
							mTimer.start();
							dialog.dismiss();
						}
					});
			return builder.create();
    		case DIALOG_WINNER:
    			builder
	    			.setCancelable(false)
					.setTitle("Winner!")
					.setMessage("Team "+ team +" won!  Do you want to play again?")
					.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							mTeam1Score = 0;
							mTeam2Score = 0;
							mTeam1ScoreView.setText("0");
							mTeam2ScoreView.setText("0");
	
							mTimerView.setText(mMinutes +":00");
							mTimer.start();
							mUsedWords = new ArrayList<Integer>();
							dialog.dismiss();
						}
					})
					.setNegativeButton("No", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							finish();
						}
					});
    			return builder.create();
    		case DIALOG_WORD_LIMIT:
    			mTimer.cancel();
    			mMp.stop();

    			builder
	    			.setCancelable(false)
					.setTitle("Limit reached")
					.setMessage("You have reached the word limit for one round.  "+ team +" wins! \n\nDo you want to play again?")
					.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							mTeam1Score = 0;
							mTeam2Score = 0;
							mTeam1ScoreView.setText("0");
							mTeam2ScoreView.setText("0");
	
							mTimerView.setText(mMinutes +":00");
							mTimer.start();
							mUsedWords = new ArrayList<Integer>();
							dialog.dismiss();
						}
					})
					.setNegativeButton("No", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							finish();
						}
					});
				return builder.create();
    		default:
    			return null;
    	}
	}
}